package controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import orm.Tie;
import orm.Users;
import services.TieBiz;

@Controller
public class TieAction {

	@Autowired
	TieBiz tieBiz;
	
	//发帖
	@RequestMapping("addTie")
	public String test1(Tie tie,HttpSession session,HttpServletRequest request,HttpServletResponse response){
		tie.setUser((Users) session.getAttribute("user"));
		tieBiz.addTie(tie);
		String str = null;
		try {
		request.setCharacterEncoding("UTF-8");//只对POST方式提交的数据有效
		response.setCharacterEncoding("UTF-8");
		response.setContentType("UTF-8");
		
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("发帖["+tie.getT_title()+"]成功！");
		//return "getAllTie.spring";
		return "redirect:getAllTie.spring";
	}
	
	//
	@RequestMapping("getAllTie")
	public String test2(HttpSession session){
		session.setAttribute("alltie", tieBiz.getAll(1));
		int count = tieBiz.tieCount();
		int pagecount=count % 10 ==0 ?count/10 : count/10+1;
		session.setAttribute("pagecount", pagecount);
		return "index.jsp";
		
	}
	//
	@RequestMapping("readTie")
	public String test3(int t_id,HttpSession session){
		//
		session.setAttribute("oneTie", tieBiz.getTieById(t_id));
		//
		return "getAllReply.spring";
	}
	//
	@RequestMapping("tiepages")
	public String test4(HttpSession session, int pageIndex){
		int count = tieBiz.tieCount();
		int pagecount = count % 10 ==0 ?count/10 : count/10+1;
		
		session.setAttribute("pagecount", pagecount);
		session.setAttribute("alltie", tieBiz.getAll(pageIndex));
		session.setAttribute("pageIndex", pageIndex);
		return "index.jsp";
	}
}
